﻿using System;
using System.Web.UI;
using System.Reflection;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Collections.Generic;

using ExtraClub.Domain;
using ExtraClub.Domain.Entities;

using SubSonic;
using ExtraClub.Domain.Forms;
using ExtraClub.Domain.Framework;

namespace ExtraClub.Web.Forms.mail_message {
    public partial class module : BaseForm {
        MailTemplateCollection templates = null;
        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
        }
        protected override string SetFields() {
            string ret = "null";
            switch (CurrentCommand) {
                case ECommand.New:
                    ret = BuildForm();
                    break;
            }
            return ret;
        }

        private string BuildForm() {
            FieldCollection fs = new FieldCollection();
            fs.Control = this;
            fs.LabelKeySuffex = "-label";

            AddHiddenFieldes(ref fs, EntityId);
            templates = new MailTemplateCollection().Load();

            /* List of Templates  */
            new Field(FieldType.Select, "template-id", f =>
            {
                f.SelectOptions = GetTemplatesOptions();
                f.SelectValues = GetTemplatesValues();
            }, fs);

            /* Description  */
            new Field(FieldType.String, "desc", f =>
            {
                f.Required = true;
            }, fs);

            /* Save as Template  */
            new Field(FieldType.Boolean, "save-as-template", f =>
            {
                f.Value = "false";
            }, fs);

            /* Message */
            new Field(FieldType.Text, "message", f =>
            {
                f.Cols = 80;
            }, fs);

            return fs.JSON();
        }

        private string[] GetTemplatesOptions() {
            List<string> list = new List<string>();
            list.Add("Select Template");
            templates.GetList().ForEach(item =>
            {
                list.Add(item.Title);
            });
            return list.ToArray();
        }

        private string[] GetTemplatesValues() {
            List<string> list = new List<string>();
            list.Add("");
            templates.GetList().ForEach(item =>
            {
                list.Add(item.Id.ToString());
            });
            return list.ToArray();
        }
    }
}
